package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.UserBrowseHistory;
import org.nnxy.foodboot.mapper.UserBrowseHistoryMapper;
import org.nnxy.foodboot.service.UserBrowseHistoryService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户浏览历史Service实现类
 */
@Service
@RequiredArgsConstructor
public class UserBrowseHistoryServiceImpl extends ServiceImpl<UserBrowseHistoryMapper, UserBrowseHistory> implements UserBrowseHistoryService {

    @Override
    public boolean addBrowseHistory(Integer userId, Integer recipeId, Integer browseDuration) {
        if (userId == null || recipeId == null) {
            return false;
        }
        
        UserBrowseHistory browseHistory = new UserBrowseHistory();
        browseHistory.setUserId(userId);
        browseHistory.setRecipeId(recipeId);
        browseHistory.setBrowseTime(LocalDateTime.now());
        browseHistory.setBrowseDuration(browseDuration);
        
        return save(browseHistory);
    }

    @Override
    public UserBrowseHistory addBrowseHistoryWithReturn(Integer userId, Integer recipeId, Integer browseDuration) {
        if (userId == null || recipeId == null) {
            return null;
        }
        
        UserBrowseHistory browseHistory = new UserBrowseHistory();
        browseHistory.setUserId(userId);
        browseHistory.setRecipeId(recipeId);
        browseHistory.setBrowseTime(LocalDateTime.now());
        browseHistory.setBrowseDuration(browseDuration);
        
        boolean success = save(browseHistory);
        
        return success ? browseHistory : null;
    }

    @Override
    public List<UserBrowseHistory> getUserBrowseHistoryList(Integer userId, int limit) {
        if (userId == null) {
            return List.of();
        }
        
        LambdaQueryWrapper<UserBrowseHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBrowseHistory::getUserId, userId)
                .orderByDesc(UserBrowseHistory::getBrowseTime)
                .last("LIMIT " + limit);
        
        return list(queryWrapper);
    }

    @Override
    public Page<UserBrowseHistory> getUserBrowseHistoryPage(Page<UserBrowseHistory> page, Integer userId) {
        if (userId == null) {
            return new Page<>();
        }
        
        LambdaQueryWrapper<UserBrowseHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBrowseHistory::getUserId, userId)
                .orderByDesc(UserBrowseHistory::getBrowseTime);
        
        return page(page, queryWrapper);
    }

    @Override
    public boolean deleteUserBrowseHistory(Integer id, Integer userId) {
        if (id == null || userId == null) {
            return false;
        }
        
        LambdaQueryWrapper<UserBrowseHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBrowseHistory::getId, id)
                .eq(UserBrowseHistory::getUserId, userId);
        
        return remove(queryWrapper);
    }

    @Override
    public boolean clearUserBrowseHistory(Integer userId) {
        if (userId == null) {
            return false;
        }
        
        LambdaQueryWrapper<UserBrowseHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBrowseHistory::getUserId, userId);
        
        return remove(queryWrapper);
    }
    
    @Override
    public boolean updateBrowseDuration(Integer id, Integer browseDuration) {
        if (id == null || browseDuration == null) {
            return false;
        }
        
        LambdaUpdateWrapper<UserBrowseHistory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserBrowseHistory::getId, id)
                .set(UserBrowseHistory::getBrowseDuration, browseDuration);
        
        return update(updateWrapper);
    }
} 